<?php

declare(strict_types=1);

namespace App\Models;

use DB;
use Illuminate\Database\Eloquent\Model;
use Log;

class HomepageCombo extends Model
{
    public $timestamps = false;

    protected $table = 'homepage_combo';

    public static function getCityCombo($cityId = 0, $withHidden = false, $sortBy = '')
    {
        $time = time();
        // DB::connection()->enableQueryLog();
        $query =
        self::select('*')->
        where(function ($query) use ($cityId, $time) {
            $query->
            where(function ($query) use ($time) {
                $query->where(['start_datetime' => 0])->orWhere([['start_datetime', '<=', $time]]);
            })->
            where(function ($query) use ($time) {
                $query->where(['end_datetime' => 0])->orWhere([['end_datetime', '>=', $time]]);
            });
        });
        if ($cityId) {
            $query->where(['city_id' => $cityId]);
        }
        if (!$withHidden) {
            $query->where(['hidden' => 0]);
        }
        if ($sortBy) {
            [$sortCol, $sortDirect] = explode(',', $sortBy);
            $query->orderBy($sortCol, $sortDirect);
        } else {
            $query->orderBy('sort_weight', 'desc');
        }

        return $query->get();
        // Log::info(DB::getQueryLog());
    }
}
